package com.ruoyi.baseData.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.baseData.domain.*;
import com.ruoyi.baseData.domain.Process;
import com.ruoyi.baseData.domain.ProcessRel;
import com.ruoyi.baseData.domain.dto.ProcessRelDto;
import com.ruoyi.baseData.domain.vo.MaterialVo;
import com.ruoyi.baseData.domain.vo.ProcessRelVo;
import com.ruoyi.baseData.domain.vo.ShiftVo;
import com.ruoyi.baseData.domain.vo.ProcessRelVo;
import com.ruoyi.baseData.mapper.*;
import com.ruoyi.baseData.service.ProcessRelService;
import com.ruoyi.baseData.mapper.ProcessRelMapper;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class ProcessRelServiceImpl implements ProcessRelService{

    @Resource
    ProcessRelMapper processRelMapper;

    @Resource
    ProceduresMapper proceduresMapper;


    @Override
    public Page<ProcessRelVo> getProcessRelByPage(ProcessRelDto pageEntity) {
        MPJLambdaWrapper<ProcessRel> wrapper = new MPJLambdaWrapper<ProcessRel>()
                .selectAll(ProcessRel.class)//查询user表全部字段
                .selectAs( Process::getName, ProcessRelVo::getProcessName )
                .selectAs(Procedures::getName, ProcessRelVo::getProceduresName)
                //记的测试阶段
                .eq(ProcessRel::getDelFlag, 0)
                .eq(ProcessRel::getProcessId,pageEntity.getProcessId())

                .leftJoin(Process.class, Process::getId, ProcessRel::getProcessId)
                .leftJoin(Procedures.class, Procedures::getId, ProcessRel::getProceduresId)
                .eq(null != pageEntity.getProcessName(),Process::getName, pageEntity.getProcessName())
                .eq(null != pageEntity.getProceduresName(), Procedures::getName, pageEntity.getProceduresName())
                .orderByDesc(ProcessRel::getProcessId)
                .orderByAsc(ProcessRel::getSequence);
        wrapper = QueryHelp.getPredicateEx(wrapper, ProcessRel.class, pageEntity);

        Page<ProcessRelVo> userList = processRelMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   ProcessRelVo.class, wrapper);
        return userList;
    }

    @Override
    public void addProcessRel(ProcessRelDto testDto) throws ParseException {

        if(null == testDto.getProceduresIds() || testDto.getProceduresIds().size() <= 0) {
            throw new GlobalException("请先选择工序");
        }
        /**
         * 查询条件
         */
        MPJLambdaWrapper<Procedures> wrapper = new MPJLambdaWrapper<Procedures>()
                .selectAll(Procedures.class)//查询user表全部字段
                .in(Procedures::getId, testDto.getProceduresIds())
                //记的测试阶段
                .eq(ProcessRel::getDelFlag, 0);
        List<Procedures> procedureses = proceduresMapper.selectList(wrapper);
        String userName = SecurityUtils.getUsername();
        for(Procedures procedures : procedureses) {
            ProcessRel test2 = new ProcessRel();
            BeanUtils.copyProperties(testDto, test2, new String[]{"id"});
            test2.setProcessId(testDto.getProcessId());
            test2.setProceduresId(procedures.getId());
            test2.setProceduresName(procedures.getName());
            test2.setDelFlag(false);
            test2.setCreateBy(userName);
            test2.setCreateTime(new Date());
            processRelMapper.insert(test2);
        }
//        MPJLambdaWrapper<ProcessRel> wrapper = new MPJLambdaWrapper<ProcessRel>()
//                .eq(ProcessRel::getCode, testDto.getCode())
//                .ne(ProcessRel::getDelFlag, 1);
//        ProcessRel material = processRelMapper.selectOne(wrapper);
//        if(null != material) {
//            throw new GlobalException("该material已经存在");
//        }
    }

    @Override
    public void updateProcessRel(ProcessRelDto testDto) {
        MPJLambdaWrapper<ProcessRel> wrapper = new MPJLambdaWrapper<ProcessRel>()
                .eq(ProcessRel::getId, testDto.getId());
        ProcessRel material = processRelMapper.selectOne(wrapper);
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        BeanUtils.copyProperties(testDto, material);
        material.setCreateBy("admin");
        material.setCreateTime(new Date());
        processRelMapper.updateById(material);
    }

    @Override
    public void delProcessRel(ProcessRelDto testDto) {
        ProcessRel material = processRelMapper.selectById(testDto.getId());
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        material.setDelFlag(true);
        processRelMapper.updateById(material);
    }

    @Override
    public void saveProcessSequence(List<ProcessRelDto> testDto) {
        for(int i =0 ; i<testDto.size(); i++) {
            ProcessRel material = processRelMapper.selectById(testDto.get(i).getId());
            material.setSequence(i + 1);
            processRelMapper.updateById(material);
        }
    }


}




